package com.weiz.config;


import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class SnowflakeConfig {
    @Value("${hutool.snowflake.worker-id:0}")
    private long workerId;
    @Value("${hutool.snowflake.data-center-id:0}")
    private long dataCenterId;
    @Bean
    public Snowflake snowflake() {
        try {
            if (workerId == 0) {
                workerId = NetUtil.getLocalHardwareAddress().hashCode() & 15;
            }
            return IdUtil.createSnowflake(workerId, dataCenterId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("Snowflake初始化失败");
        }
    }


}